package com.mlx.mbg.code.method;

import com.mlx.mbg.code.inter.EnableMethod;
import com.mlx.mbg.code.config.MbgPropertyConfig;
import com.mlx.mbg.code.inter.ExtensionLevel;
import com.mlx.mbg.code.inter.IExtensionMethod;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.Collections;
import java.util.List;

@EnableMethod
public class ListSelectiveMethod implements IExtensionMethod {
    @Override
    public boolean checkConfig(ExtensionLevel level, MbgPropertyConfig mbgPropertyConfig, IntrospectedTable introspectedTable) {
        return getExtensionLevelSet().contains(level) && mbgPropertyConfig.getEnableListSelective();
    }

    @Override
    public int getOrder() {
        return 1200;
    }

    @Override
    public List<Method> buildMethod(IntrospectedTable introspectedTable) {
        FullyQualifiedJavaType modelType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        // 访问类型：public
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        // 返回类型：List
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("List");
        // 设置泛型参数
        returnType.addTypeArgument(modelType);

        // 方法名称
        String methodName = "listSelective";

        // 设置参数类型是对象
        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        Parameter parameter = new Parameter(parameterType, "query");

        // 创建方法对象
        Method method = new Method();
        // 设置访问类型
        method.setVisibility(methodVisibility);
        // 设置返回类型对象
        method.setReturnType(returnType);
        // 设置方法名
        method.setName(methodName);
        // 设置方法参数
        method.addParameter(parameter);
        // 设置备注
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 动态查询");
        method.addJavaDocLine(" */");

        return Collections.singletonList(method);
    }

    @Override
    public List<XmlElement> buildSQL(IntrospectedTable introspectedTable) {
        XmlElement where = new XmlElement("where");

        XmlElement includeProperty = new XmlElement("include");
        includeProperty.addAttribute(new Attribute("refid", "Select_Selective_Property_List"));
        where.addElement(includeProperty);

        XmlElement listSelective = new XmlElement("select");
        listSelective.addAttribute(new Attribute("id", "listSelective"));
        listSelective.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        TextElement text3 = new TextElement("select");
        XmlElement baseInclude = new XmlElement("include");
        baseInclude.addAttribute(new Attribute("refid", "Base_Column_List"));
        TextElement text4 = new TextElement("from " + introspectedTable.getFullyQualifiedTableNameAtRuntime());
        listSelective.addElement(text3);
        listSelective.addElement(baseInclude);
        listSelective.addElement(text4);
        listSelective.addElement(where);

        return Collections.singletonList(listSelective);
    }
}
